package com.ztgf.shopping.api.cms.epg;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.epg.EpgNoticeBO;
import com.ztgf.shopping.models.bo.epg.EpgNoticeSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgNoticeDTO;
import com.ztgf.shopping.models.service.epg.IEpgNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 公告消息  前端控制器
 * </p>
 *
 * @author YUE
 * @since 2020-05-20
 */
@RestController
@RequestMapping("/cms/epgNotice")
@Slf4j
@Api(value = "EpgNoticeController",tags = "公告消息接口")
public class EpgNoticeController {

    @DubboReference
    private IEpgNoticeService iEpgNoticeService;

    @ApiOperation(value = "添加公告消息",notes = "添加公告消息")
    @PostMapping(value = "/insert")
    public Result insert( EpgNoticeSaveBO epgNoticeSaveBO) {
        boolean flag = iEpgNoticeService.add(epgNoticeSaveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }


    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<EpgNoticeDTO> get(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        EpgNoticeDTO epgNoticeDTO = iEpgNoticeService.selectById(id);
        return new Result<>(epgNoticeDTO);
    }


    @PostMapping("/save")
    @ApiOperation(value = "保存公告消息",notes = "保存公告消息")
    public Result save( EpgNoticeSaveBO epgNoticeSaveBO){
        if(epgNoticeSaveBO.getId() == null){
            //添加
            return  insert(epgNoticeSaveBO);
        } else {
            return  update(epgNoticeSaveBO);
        }
    }

    @ApiOperation(value = "修改公告消息",notes = "修改公告消息")
    @PostMapping("/update")
    public Result update( EpgNoticeSaveBO epgNoticeSaveBO){
        EpgNoticeDTO epgNoticeDTO = new EpgNoticeDTO();
        BeanUtils.copyProperties(epgNoticeSaveBO, epgNoticeDTO);
        epgNoticeDTO.setId(epgNoticeSaveBO.getId());
        boolean flag = iEpgNoticeService.updateById(epgNoticeDTO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除公告消息",notes = "删除公告消息")
    @ApiImplicitParam(name = "id",value = "公告消息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = iEpgNoticeService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除公告消息",notes = "批量删除公告消息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        iEpgNoticeService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();

    }

    @ApiOperation(value = "公告消息列表",notes = "公告消息列表")
    @PostMapping("/queryPageList")
    public Result<Page<EpgNoticeDTO>> queryPageList(EpgNoticeBO epgNoticeBO){
        Wrapper<EpgNoticeDTO> wrapper = new EntityWrapper<>();
        Page<EpgNoticeDTO> page = new Page<>(epgNoticeBO.getPageNo(), epgNoticeBO.getPageSize());
        page = iEpgNoticeService.selectPage(page, wrapper);
        return new Result<>(page);
    }

}

